package storage.v1;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.carrotsearch.hppc.IntOpenHashSet;
import com.onpositive.mediawiki.redirects.WikipediaRefsRedirector;
import com.onpositive.wiki3.db.ITextContentAccess;
import com.onpositive.wiki3.db.ITitleMapper;
import com.onpositive.wiki3.db.IWikiDB;
import com.onpositive.wiki3.db.impl.CategoryAccessImpl;

public class BaseBasedTextReferencesStorage {
	
	private String localPath = "C:/wikistorage/" ;
	
	IWikiDB db=WikipediaRefsRedirector.wikiDBImpl;
	ITitleMapper mapper=WikipediaRefsRedirector.titleMapper;
	ITextContentAccess content=WikipediaRefsRedirector.accessor;
	
	public BaseBasedTextReferencesStorage(String path) {
		localPath  = path.trim() ;
		localPath += localPath.endsWith("/") ? "" : "/" ; 
		localPath += "raw text storage/";		
	}
	
	public String getContent( String id, int flags )
	{
		if( id == null )
			return null ;
		
		String result = null ;
		return result != null ? result : loadGlobal( id , flags ) ;		
	}	
	

	public String loadGlobal( String id , int flags ) {
		int id2 = mapper.getId(id);
		if (id2==-1){
			return null;
		}
		int redirect = db.getRedirectAccess().getRedirect(id2);
		if (redirect!=-1){
			id2=redirect;
		}
		if (content.hasText(id2)){
			String textContent = content.getTextContent(id2);
			return textContent;	
		}
		return "";
	}
	HashMap<String, HashSet<String>>chs=new HashMap<String, HashSet<String>>();
	HashMap<String, HashSet<String>>str=new HashMap<String, HashSet<String>>();
	
	public Collection<String> getChildren( String id )
	{
		if (chs.containsKey(id)){
			return chs.get(id);
		}
		Set<String> categoryMembersAsTitles = db.getCategoryAccessor().getCategoryMembersAsTitles(mapper.getId(id), false);
		chs.put(id,new HashSet<String>(categoryMembersAsTitles) );
		return categoryMembersAsTitles;		
	}
	
	public Collection<String> getParents( String id )
	{
		if (str.containsKey(id)){
			return str.get(id);
		}
		Set<String> categoryMembersAsTitles = db.getCategoryAccessor().getParentCategoriesAsTitles(mapper.getId(id), false);
		str.put(id,new HashSet<String>(categoryMembersAsTitles) );
		return categoryMembersAsTitles;		
				
	}

	public boolean containsId( String id ) {
		return id != null && mapper.getId(id)!=-1;
	}


	public String getRootPath() {
		return localPath;
	}

	public void setRootPath(String rootPath) {
		this.localPath = rootPath;
	}

	public HashMap<String, Set<String>> getParents(HashSet<String> str2) {
		IntOpenHashSet op=new IntOpenHashSet();
		for (String s:str2){
			int id = mapper.getId(s);
			op.add(id);
		}
		Map<Integer, IntOpenHashSet> categoryMembers = ((CategoryAccessImpl)db.getCategoryAccessor()).getCategoryMembers(op, false);
		HashMap<String, Set<String>>str=new HashMap<String, Set<String>>();
		for (Integer i:categoryMembers.keySet()){
			String title = mapper.getTitle(i);
			IntOpenHashSet intOpenHashSet = categoryMembers.get(i);
			HashSet<String>ss=new HashSet<String>();
			for (int a:intOpenHashSet.toArray()){
				ss.add(mapper.getTitle(a));
			}
			str.put(title,ss);
		}
		return str;		
	}
	
}
